{template '_header'}
<script>document.title = "选择兑换商品"; </script>
<form action="{php echo mobileUrl('exchange/calculate',array(),1);}" method="post" class="form-horizontal form-validate" novalidate="novalidate" data-status="0">
<div id="status">
<div class='fui-page fui-page-current'>
    <div class="fui-content">
        {if !empty($banner)}
        <div class="fui-swipe" style="height: 200px">
            <div class="fui-swipe-wrapper">
                {loop $banner $k $v}
                <div class="fui-swipe-item"><img src="{php echo tomedia($v);}" style="height: 100%"></div>{/loop}
            </div>
            <div class="fui-swipe-page"></div>
        </div>
        {/if}
        <div class="fui-card">
            <div class="fui-card-content" style="margin-left: 50px">
                您的兑换码是 <b style="color: #ff445f;">{$key}</b><br>
                {if $groupResult['showcount'] == 0}此兑换码被查询了{$codeResult['count']} 次 <br>{/if}
                您可以凭兑换码兑换 {if $groupResult['type'] == 1}{if $groupResult['max']>0}{$groupResult['max']}{else}任意{/if} 件{else}价值 <span style="color: #ff445f;">{$groupResult['value']}</span> 元的{/if}商品 <br>
                已选 <span style="color: #ff445f;" class="total">0</span> 件{if $groupResult['type']==1}还可以再选 <span style="color: #ff445f;" class="again"> {if $groupResult['max']>0} {$groupResult['max']} {else}任意{/if}</span> 件 {/if}<br>
            </div>

        </div>
        <div class="fui-goods-group block three border" style="padding-bottom: 70px">
{loop $goodsList $key $value}
            <input type="hidden" name="goods[]" id="goods{$value['id']}" value="">
            <div class="fui-goods-item" data-goodsid="539">
                <a href="{php echo mobileUrl('goods/detail',array('id'=>$value['id']),1);}"><div class="image" data-lazyloaded="true" style="background-image: url({php echo tomedia($value['thumb']);});"></div></a>
                <div class="detail">
                    <div class="name">{$value['title']}</div>
                    <div class="price">
                        <span class="text">￥{$value['marketprice']}</span>
                    </div>
                </div>
                    <div class="btn btn-default btn-sm block x" type="default" data="{$value['id']}" value="{$value['marketprice']}" onclick="initialize();">选择</div>
            </div>
{/loop}
{loop $optionList $key $value}
            <input type="hidden" name="option[]" id="option{$value['id']}" value="">
            <div class="fui-goods-item" data-goodsid="539">
                <a href="{php echo mobileUrl('goods/detail',array('id'=>$value['id']),1);}"><div class="image" data-lazyloaded="true" style="background-image: url({php echo tomedia($value['thumb']);});"></div></a>
                <div class="detail">
                    <div class="name">{$value['title']}</div>
                    <div class="price">
                        <span class="text">￥{$value['marketprice']}</span>
                    </div>
                </div>
                <a class="btn btn-default btn-sm block order-verify" value="{php echo mobileUrl('exchange/modal',array('goods'=>$value['id'],'option'=>$value['optionstr']),1);}" data="{$value['id']}" onclick="initialize();">选择</a>
            </div>
{/loop}
        </div>
    </div>
        <input type="hidden" name="total" value="">
        <input type="hidden" name="value" value="0">
</div>
</div>
        <div class="fui-navbar" style="position: absolute;bottom: 0px" id="nav">
            <button type="submit" class="btn btn-danger block" style="width: 96%">立即兑换（共<span class="total">0</span>件商品，需付<span class="value">0</span>元）</button>
        </div>



</form>
<script language="javascript">
    var goodsArr = new Array();
    var optionArr = new Array();
    var optionsss = new Array();
    var exchangetype = {$groupResult['type']};
    var exchangevalue = {if $groupResult['type'] == 2}{$groupResult['value']}{else}{$groupResult['max']}{/if};
    require(['../addons/ewei_shopv2/plugin/exchange/static/js/op.js'], function (modal) {
        modal.init();
    });
    
    function initialize() {
        if($("form").attr('data-status') == '0'){
            $("input[name='total']").val('0');
            $("input[name='value']").val('0');
            $("input[name='option[]']").val('');
            $("input[name='goods[]']").val('');
            $("form").attr('data-status','1');
        }
    }

    $('#status').click(function () {
        initialize();
    });

    $('#nav').click(function () {
        initialize();
        var amount = Number($("input[name='total']").val());
        if (amount==0){
            $("form").attr('onsubmit','return checkInput();');
            return;
        }else{
            $("form").attr('onsubmit','');
        }

        var money = Number($(".value").text());

        $("form").attr('onsubmit','return checkMoney();');



    });
    
    function checkInput() {
        FoxUI.alert("未选择任何商品");
        return false;
    }
    function checkMoney() {
        var money = Number($(".value").text());
        if (money==0){
            FoxUI.confirm("",'确定兑换所选商品吗?',function () {
                $('form').attr('data-status','0');
                $("form").attr('onsubmit','');
                $('form').submit();
            });
        }else if(money>0){
            FoxUI.confirm("确定支付"+money+"元兑换所选商品吗?",'',function () {
                $('form').attr('data-status','0');
                $("form").attr('onsubmit','');
                $('form').submit();
            });
        }else{
            return false;
        }

        return false;
    }
</script>



{template '_footer'}